package taller2.view.windows;

import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JFrame;

import taller2.interfaces.HidableWindow;
import taller2.utils.Constants;
import taller2.utils.Point;
import taller2.utils.Utils;


public class MyWindow extends JFrame implements HidableWindow{
	private static final long serialVersionUID = 1L;
	private ArrayList<Component> components;
	private GridBagLayout layout;
	public MyWindow(String title){
		super(title);
		
		layout = new GridBagLayout();
		components = new ArrayList<Component>();
		
		setLayout(layout);
		setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setVisible(true);	
	}
	
	// redraws window
	public void redraw(){
		setVisible(true);
		repaint();
	}
	
	// lets you add components to the window in the desired position
	public void addComponent(Component component, double x, double y, Point pos) {
		Utils.addComponent(this,component,x,y,pos);
		components.add(component);
	}
	
	public void removeComponent(Component component) {
		this.remove(component);
		repaint();
	}
	
	//changes the window to ocupy half the screen
	public void fullScreen() {
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		Dimension tam = Toolkit.getDefaultToolkit().getScreenSize();
		changeSize((int) (tam.width*Constants.padding),(int) (tam.height*Constants.padding));		
	}	
	
	//changes the window to ocupy half the screen
	public void halfScreen() {
		Dimension tam = Toolkit.getDefaultToolkit().getScreenSize();
		changeSize((int)(tam.width*Constants.half),(int)(tam.height*Constants.padding));
	}		
	
	// changes the size of the window
	public void changeSize(int tamX,int tamY){
		setSize(new Dimension(tamX,tamY));
	}
	
	//changes default behabiour when closing
	public void hideOnClose() {
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	}
	
	// centers window on screen
	public void center(){
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width/2-100,dim.height/2-100);
	}
}
